package com.weishui.downloading.app;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import com.jtattoo.plaf.mcwin.McWinLookAndFeel;
import com.weishui.downloading.listener.ButtonActionListener;
import com.weishui.downloading.util.Constants;
import com.weishui.downloading.util.LimitedDocument;

public class DownLaodWindow extends AbstractPanle{

	public JTextField textFieldProxyPort;
	public JIpAddressField jIpAddressField;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public JTextField textFieldDir;
	public JTextField textFieldThreadCount;
	public JTextField textFieldSourceUrl;
	private JFrame frame;
	
	public static boolean downLoadWithProxy = false;

	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			UIManager.setLookAndFeel(new McWinLookAndFeel());
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DownLaodWindow window = new DownLaodWindow(new JFrame(), true);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application
	 */
	public DownLaodWindow(JFrame owner, boolean modal) {
		super(owner, true);
		createContents();
	}

	/**
	 * Initialize the contents of the frame
	 */
	private void createContents() {
		frame = new JFrame();
		frame.getContentPane().setLayout(null);
		frame.setBounds(100, 100, 679, 582);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "配置信息", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel.setBounds(10, 10, 654, 279);
		frame.getContentPane().add(panel);

		final JLabel labelURL = new JLabel();
		labelURL.setText("源文件URL:");
		labelURL.setBounds(25, 65, 66, 18);
		panel.add(labelURL);

		textFieldSourceUrl = new JTextField();
		textFieldSourceUrl.setEditable(true);
		textFieldSourceUrl.setBounds(100, 65, 414, 22);
		textFieldSourceUrl.setText(Constants.DOWN_LOAD_URL);
		panel.add(textFieldSourceUrl);

		final JLabel label = new JLabel();
		label.setText("下载线程数:");
		label.setBounds(25, 102, 73, 18);
		panel.add(label);
		//可输入文本框限制条件
		LimitedDocument limitedDocument = new LimitedDocument(2);
		limitedDocument.setAllowChar("0123456789");
		
		textFieldThreadCount = new JTextField();
		textFieldThreadCount.setBounds(99, 100, 414, 22);
		textFieldThreadCount.setDocument(limitedDocument);
		textFieldThreadCount.setText(Constants.DOWN_LOAD_COUNT);
		panel.add(textFieldThreadCount);

		final JLabel labelDir = new JLabel();
		labelDir.setText("下载目录:");
		labelDir.setBounds(35, 27, 55, 18);
		panel.add(labelDir);

		textFieldDir = new JTextField();
		textFieldDir.setEditable(false);
		textFieldDir.setBounds(100, 25, 414, 22);
		textFieldDir.setText(Constants.DOWN_LOAD_DIR);
		panel.add(textFieldDir);

		final JButton buttonSelectDir = new JButton();
		buttonSelectDir.setActionCommand("selectDir");
		buttonSelectDir.setText("浏览");
		buttonSelectDir.setBounds(535, 25, 84, 21);
		buttonSelectDir.addActionListener(new ButtonActionListener(this,textFieldDir));
		panel.add(buttonSelectDir);

		final JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(null, "代理服务器", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel_1.setBounds(27, 142, 486, 99);
		panel.add(panel_1);

		final JCheckBox checkBoxProxy = new JCheckBox();
		checkBoxProxy.setText("使用代理");
		checkBoxProxy.setBounds(10, 26, 89, 26);
		if("1".equals(Constants.USE_PROXY)){
			checkBoxProxy.setEnabled(true);
			downLoadWithProxy = true;
		}
		checkBoxProxy.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				// TODO Auto-generated method stub
				if(downLoadWithProxy){
					downLoadWithProxy = false;
					checkBoxProxy.setSelected(false);
					jIpAddressField.setEnabled(false);
					textFieldProxyPort.setEnabled(false);
				}else{
					downLoadWithProxy = true;
					checkBoxProxy.setSelected(true);
					jIpAddressField.setEnabled(true);
					textFieldProxyPort.setEnabled(true);
				}
			}
		});
		panel_1.add(checkBoxProxy);

		final JLabel ipLabel = new JLabel();
		ipLabel.setText("IP:");
		ipLabel.setBounds(44, 58, 14, 18);
		panel_1.add(ipLabel);

		jIpAddressField = new JIpAddressField();
		jIpAddressField.setBounds(72, 58, 153, 22);
		jIpAddressField.setEnabled(false);
		jIpAddressField.setText(Constants.PROXY_IP);
		panel_1.add(jIpAddressField);

		final JLabel labelPort = new JLabel();
		labelPort.setText("PORT:");
		labelPort.setBounds(240, 58, 42, 18);
		panel_1.add(labelPort);

		// 允许长度为2
		
		textFieldProxyPort = new JTextField();
		textFieldProxyPort.setBounds(286, 58, 77, 25);
		textFieldProxyPort.setEnabled(false);
		textFieldProxyPort.setText(Constants.PROXY_PORT);
		LimitedDocument limitDocument = new LimitedDocument(5);
		// 只允许输入数字
		limitDocument.setAllowChar("0123456789");
		textFieldProxyPort.setDocument(limitDocument);
		panel_1.add(textFieldProxyPort);

		final JButton buttonStartDownLoad = new JButton();
		buttonStartDownLoad.setText("开始下载");
		buttonStartDownLoad.setBounds(550, 305, 86, 21);
		buttonStartDownLoad.setActionCommand("startdownLoad");
		buttonStartDownLoad.addActionListener(new ButtonActionListener(textFieldDir,textFieldSourceUrl,textFieldThreadCount,jIpAddressField,textFieldProxyPort));
		frame.getContentPane().add(buttonStartDownLoad);

		final JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "日志", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel_2.setLayout(new BorderLayout());
		panel_2.setBounds(10, 337, 651, 201);
		panel_2.setPreferredSize(new Dimension(0, 200));
		panel_2.add(jsPan);
		frame.getContentPane().add(panel_2);
	}

}
